package com.axsun.service_comments.service;


import com.axsun.service_comments.conversion.Data;
import com.axsun.service_comments.conversion.Rep;
import com.axsun.service_comments.conversion.types.CommentRepList;
import com.axsun.service_comments.conversion.types.InfoRepList;
import com.axsun.service_comments.conversion.types.RepList;
import com.axsun.service_comments.conversion.types.ReplyList;
import com.axsun.service_comments.dao.UDao;
import com.axsun.service_comments.entity.Comments;
import com.axsun.service_comments.entity.Info;
import com.axsun.service_comments.entity.Reply;
import com.axsun.service_comments.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UService {

    private UDao uDao;

    @Autowired
    public void setuDao(UDao uDao) {
        this.uDao = uDao;
    }

    public List<User> showDao(int age) {
        return uDao.get(age);
    }

    public String insert(String name, int age) {
        User user = new User();
        user.setName(name);
        user.setAge(age);
        uDao.insert(user);
        return "Insert ( \"" + name + "\", age" + age + ") OK!";
    }

    public void insertComments() {
        var comments = new Comments();
        comments.setContent("这是插入的contnent");
        comments.setLikes(12);
        comments.setMaintain_id(1);
        comments.setTitle("this is title");
        uDao.insertComments(comments);
    }

    public void insertInfo() {
        var info = new Info();
        info.setId(1);
        info.setFrom_id(1);
        info.setLike_num(12);
        info.setContent("ssss");
        info.setComments_id(1);
        uDao.insertInfo(info);
    }


    public List<Info> getAllInfos() {
        return uDao.getAllInfos();
    }

    public List<Reply> getAllReplies() {
        return uDao.getAllReplies();
    }

    public void insertReply() {
        System.out.println("Inserting reply");
        var reply = new Reply();
        reply.setId(2);
        reply.setComment_id(2);
        reply.setFrom_id(2);
        reply.setTo_id(2);
        reply.setInfo_id(2);
        reply.setContent("this is two");
        uDao.insertReply(reply);
    }


    public Rep ShowSpecInfos(int key) {
        /**
         * do sth here for example make the data packagesssss
         * @author: peter
         *
         */

        int code = 1000;
        String message = "查看评论成功";
        Data data = new Data();

        // 设置data
        {

            // info的数目 ??? 不完美

            var list = uDao.ShowSpecInfos(key);
            data.setCount(1);
            data.setTotal(list.size());
            List<RepList> infoList = new ArrayList<>();
            {


                for (var info : list) {
                    // 设置info
                    var tmp_info = new InfoRepList();
                    tmp_info.setLikes(info.getLike_num());
                    tmp_info.setId(info.getId());
                    tmp_info.setUserLogo("https://b-ssl.duitang.com/uploads/item/201602/12/20160212184151_5vzxW.jpeg");
                    tmp_info.setContent(info.getContent());
                    tmp_info.setImgId("xcclsscrt0tev11ok364");
                    tmp_info.setCreateDate(info.getCreate_time());
                    tmp_info.setNickName(info.getName());
                    // to be continue
                    tmp_info.setReplyTotal(0);
                    // 设置每个info的reply
                    var replyList = uDao.getSpecReplies(info.getComments_id(), info.getId());
                    if (replyList != null) {
                        // 数据的格式转换
                        List<ReplyList> added = new ArrayList<>();
                        for (var k : replyList) {
                            var tmp = new ReplyList();
                            tmp.setNickName(k.getName());
                            tmp.setUserLogo("https://b-ssl.duitang.com/uploads/item/201602/12/20160212184151_5vzxW.jpeg");
                            tmp.setId(k.getId());
                            tmp.setCommentId(k.getComment_id());
                            tmp.setContent(k.getContent());
                            tmp.setStatus(11);
                            tmp.setCreateDate(k.getCreate_time());
                            added.add(tmp);
                        }
                        // 设置reply的东西
                        tmp_info.setReplyTotal(replyList.size());
                        tmp_info.setReplyList(added);
                    }

                    // 把info扩充
                    infoList.add(tmp_info);
                }
            }

            data.setList(infoList);

        }

        //设置 code messag data


        Rep rep = new Rep();
        rep.setCode(code);
        rep.setMessage(message);
        rep.setData(data);

        return rep;
    }


    public Rep showAllComments() {
        var rep = new Rep();
        var list = uDao.showAllComments();

        if (list == null) {
            rep.setCode(1002);
            rep.setMessage("无帖子");
            rep.setData(null);
        } else {

            rep.setCode(1001);
            rep.setMessage("查看帖子成功");

            var data = new Data();
            data.setCount(1);
            data.setTotal(list.size());

            List<RepList> commentRepListList = new ArrayList<>();

            for (var i : list) {
                var dataList = new CommentRepList();
                dataList.setId(i.getId());
                dataList.setNickName(i.getName());
                dataList.setUserLogo("https://tva2.sinaimg.cn/mw1024/" +
                        "006yt1Omgy1ge41ryif1ij315o0tinpd.jpg");
                dataList.setTitle(i.getTitle());
                dataList.setContent(i.getContent());
                dataList.setCreateDate(i.getCreate_date());

                dataList.setReplyCount(uDao.getReplyCount(i.getId()));


                dataList.setLikes(i.getLikes());
                dataList.setRefs("./rep.json");
                dataList.setImgId("xcclsscrt0tev11ok364");
                commentRepListList.add(dataList);
            }

            data.setList(commentRepListList);

            rep.setData(data);
        }

        return rep;
    }
}

